package com.kc.saf.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kc.saf.domain.CoreManagement;
import com.kc.saf.domain.CoreType;
import com.kc.saf.service.CoreManagementService;
import com.kc.saf.service.CoreTypeService;
import com.kc.saf.vo.CoreManagementVo;
import com.kc.saf.vo.WebPageVo;
import com.kc.sys.common.DataGridView;
import com.kc.sys.common.R;
import com.kc.sys.domain.User;
import com.kc.sys.util.UUIDUtil;
import com.kc.sys.util.WebUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 老杜
 * @since 2019-11-08
 */
@RestController
@RequestMapping("/coreManagement")
public class CoreManagementController {

    @Autowired
    private CoreManagementService coreManagementService;
    @Autowired
    private CoreTypeService coreTypeService;

    /**
     * 查询
     */
    @RequestMapping("loadAllCoreManagement")
    public DataGridView loadAllCoreManagement(CoreManagementVo coreManagementVo){
        //获取用户session
        //User user = (User)WebUtils.getSession().getAttribute("user");
        IPage<CoreManagement> page = new Page<>(coreManagementVo.getPage(),coreManagementVo.getLimit());
        QueryWrapper<CoreManagement> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(coreManagementVo.getCoreId()!=null&&coreManagementVo.getCoreId()!=0,"core_id",coreManagementVo.getCoreId());
        queryWrapper.like(StringUtils.isNotBlank(coreManagementVo.getCoreTitle()),"core_title",coreManagementVo.getCoreTitle());
        coreManagementService.page(page,queryWrapper);
        List<CoreManagement> coreManagements = page.getRecords();
        for (CoreManagement coreManagement : coreManagements) {
            CoreType coreType = coreTypeService.getById(coreManagement.getCoreId());
            if(null!=coreType){
                coreManagement.setCoreType(coreType.getTypename());
            }
        }
        return new DataGridView(page.getTotal(),page.getRecords());
    }



    //根据Id查询五大核心
    @RequestMapping("loadCoreById/{id}")
    public  R loadCoreById(@PathVariable("id") Integer id){
        try {
            CoreManagement coreManagement = coreManagementService.getById(id);
            return R.ok().data("item",coreManagement);
        }catch (Exception e){
            e.printStackTrace();
            return R.error().message("没有数据");
        }
    }
    //添加4大核心详情
    @RequestMapping("addCoreManagement")
    public R addCoreManagement(CoreManagementVo coreManagementVo){
        try {
            User user = (User)WebUtils.getSession().getAttribute("user");
            coreManagementVo.setAuthor(user.getName());
            coreManagementVo.setCreateDate(new Date());
            coreManagementService.save(coreManagementVo);
            return R.ok().message("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("添加失败");
        }
    }
    //更新4大核心详情
    @RequestMapping("updateCoreManagement")
    public R updateCoreManagement(CoreManagementVo coreManagementVo){
        try {
            coreManagementService.updateById(coreManagementVo);
            return R.ok().message("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("更新失败");
        }
    }
    /**
     * 根据Id 删除
     * */
    @RequestMapping("deleteCoreManagement")
    public R deleteById(Integer id){
        try {
            coreManagementService.removeById(id);
            return R.ok().message("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("删除失败");
        }
    }

    //加载最大的排序码
    @RequestMapping("loadCoreMaxOrderNum")
    public Map<String ,Object> loadCoreMaxOrderNum(){
        Map<String,Object> map = new HashMap<>();
        QueryWrapper<CoreManagement> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("ordernum");
        List<CoreManagement> list = coreManagementService.list(queryWrapper);
        if(list.size()>0){
            map.put("value",list.get(0).getOrdernum()+1);
        }else{
            map.put("value",1);
        }
        return map;
    }

    //根据安全警示类型查询
    @RequestMapping("loadCoreByType/{type}")
    public R loadCoreByType(@PathVariable("type") Integer type){
        QueryWrapper<CoreManagement> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("core_id",type);
        List<CoreManagement> list = coreManagementService.list(queryWrapper);
        return  R.ok().data("item",list);
    }
    public final static String UPLOAD_FILE_PATH = "D:\\uploadFile\\";
    @RequestMapping("/uploadFileasdasdas")
    public WebPageVo uploadFile(@RequestParam("file") MultipartFile file){
        WebPageVo webPageVo = new WebPageVo();
        String fileName = null;
        String fileNowName = null;
        Boolean flag=false;
        Map<String,Object> param=new HashMap<>();
        if(file.isEmpty()){
            webPageVo.setMsg("文件不能为空");
            webPageVo.setCode(1);
            return webPageVo;
        }
        try{
          /*  String path1 = Thread.currentThread().getContextClassLoader().getResource("").getPath();//获取当前资源的虚拟路径

            path1=path1+"upload/";*/
            String  path1= UPLOAD_FILE_PATH;
            //创建一个文件夹
            Date date = new Date();
            //文件夹的名称
            String paths=path1+new SimpleDateFormat("yyyyMMdd").format(date);
            //如果不存在,创建文件夹
            File f = new File(paths);
            if(!f.exists()){
                f.mkdirs();
                //如果文件夹不存在创建一个
                fileName = file.getOriginalFilename();//获取原名称
                fileNowName = UUIDUtil.getUUID2()+"."+ FilenameUtils.getExtension(fileName);//生成唯一的名字
                File dest = new File(paths+"/"+fileNowName);
                file.transferTo(dest);
            }else{
                fileName = file.getOriginalFilename();//获取原名称
                fileNowName = UUIDUtil.getUUID2()+"."+ FilenameUtils.getExtension(fileName);//生成唯一的名字
                File dest = new File(paths+"/"+fileNowName);
                file.transferTo(dest);
            }

            webPageVo.setMsg(fileNowName);
            webPageVo.setCode(0);
        }catch(Exception e){
            e.printStackTrace();
            webPageVo.setMsg("上传失败,请重新上传");
            webPageVo.setCode(1);
        }
        return webPageVo;
    }
}

